<?php
/** 
 * 权限分组
 *
 * @author   xg
 * @date     2019-09-20
 */

namespace app\auth\controller;

use app\base\controller\Backend;
use app\auth\model\AuthGroupModel;

class Group extends Backend
{

    protected $noNeedLogin = [];

    /**
     * 分组列表
     * @param string  $keyword  搜索关键词
     * @param number  $page     当前页码
     * @param number  $limit    每页显示条数
     * @return array
     */
    public function index()
    {
        $rule = [
            'keyword'   => 'chsDash',
            'page'      => 'number',
            'limit'     => 'number',
        ];

        $msg = [
            'keyword.chsDash'   => '搜索关键词格式错误',
            'page.number'       => '页码格式错误',
            'limit.number'      => '页数格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());

        if (!is_array($params)) {
            $this->error($params);
        }
        
        $group_model = new AuthGroupModel();
        $group_data  = $group_model->getList($params);

        if (!empty($group_model->getError())) {
            $this->error($group_model->getError());
        }

        $this->success('成功', $group_data);
    }

    /**
     * 分组添加
     * @param string  $pid      父ID
     * @param number  $name     组名
     * @param number  $rules    组权限
     * @param number  $remark   备注
     * @param number  $status   状态
     * @return array
     */
    public function add()
    {
        $rule = [
            'pid'       => 'number',
            'name'      => 'require|max:50',
            'rules'     => 'array',
            'remark'    => 'max:255',
            'status'    => 'number',
        ];

        $msg = [
            'pid.number'        => '父ID格式错误',
            'name.require'      => '组名不能为空',
            'name.max:50'       => '组名格式错误',
            'rules.array'       => '组权限格式错误',
            'remark.max:255'    => '备注格式错误',
            'status.number'     => '状态格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $group_model = new AuthGroupModel();
        $group_data  = $group_model->addOne($params);

        if (!empty($group_model->getError())) {
            $this->error($group_model->getError());
        }

        $this->success('添加成功');
    }

    /**
     * 分组修改
     * @param string  $group_id 分组ID
     * @param string  $pid      父ID
     * @param number  $name     组名
     * @param number  $rules    组权限
     * @param number  $remark   备注
     * @param number  $status   状态
     * @return array
     */
    public function edit()
    {
        $rule = [
            'group_id'  => 'require|number',
            'pid'       => 'number',
            'name'      => 'require|max:50',
            'rules'     => 'array',
            'remark'    => 'max:255',
            'status'    => 'number',
        ];

        $msg = [
            'group_id.require'  => '缺少分组ID',
            'group_id.number'   => '分组ID格式错误',
            'pid.number'        => '父ID格式错误',
            'name.require'      => '组名不能为空',
            'name.max:50'       => '组名格式错误',
            'rules.array'       => '组权限格式错误',
            'remark.max:255'    => '备注格式错误',
            'status.number'     => '状态格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $group_model = new AuthGroupModel();
        $group_data  = $group_model->editOne($params);

        if (!empty($group_model->getError())) {
            $this->error($group_model->getError());
        }

        $this->success('修改成功');
    }

    /**
     * 分组删除
     * @param string  $group_id  分组ID
     * @return array
     */
    public function del()
    {
        $rule = [
            'group_id'   => 'require|array',
        ];

        $msg = [
            'group_id.require'   => '缺少分组ID',
            'group_id.array'     => '分组ID格式错误',
        ];

        $params = $this->paramCheck($rule, $msg, $this->request->param());
        
        if (!is_array($params)) {
            $this->error($params);
        }

        $group_model = new AuthGroupModel();
        $group_data  = $group_model->delOne($params);

        if (!empty($group_model->getError())) {
            $this->error($group_model->getError());
        }

        $this->success('删除成功');
    }

}